package com.hymall.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketFootprint;
import com.hymall.bean.MarketFootprintExample;
import com.hymall.bean.MarketGoods;
import com.hymall.bean.MarketGoodsExample;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.vo.WxFootPrintVo;
import com.hymall.mapper.MarketFootprintMapper;
import com.hymall.mapper.MarketGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: 牟治宇
 * @description 历史足迹实现类
 * @date: 2022/7/19 10:14
 */

@Service
public class FootprintServiceImpl implements FootprintService {

    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public Map<String, Object> insertFootprint(Integer page, Integer limit, Integer userId) {

        HashMap map = new HashMap();

        PageHelper.startPage(page, limit);
        PageHelper.orderBy("update_time desc");

        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        // MarketGoodsExample marketGoodsExample = new MarketGoodsExample();

        marketFootprintExample.createCriteria().andUserIdEqualTo(userId);
        marketFootprintExample.createCriteria().andDeletedEqualTo(false);

        // MarketFootprint marketFootprint = marketFootprintMapper.selectByPrimaryKey(userId);

        List<MarketFootprint> footprints = marketFootprintMapper.selectByExample(marketFootprintExample);

        ArrayList<Map> mapArrayList = new ArrayList<>();

        for (MarketFootprint footprint : footprints) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("id", footprint.getId());
            objectObjectHashMap.put("addTime", footprint.getAddTime());
            objectObjectHashMap.put("goodsId", footprint.getGoodsId());
            objectObjectHashMap.put("brief", marketGoods.getBrief());
            objectObjectHashMap.put("picUrl", marketGoods.getPicUrl());
            objectObjectHashMap.put("retailPrice", marketGoods.getRetailPrice());
            objectObjectHashMap.put("name", marketGoods.getName());
            mapArrayList.add(objectObjectHashMap);
        }
        PageInfo<MarketFootprint> marketFootprintPageInfo = new PageInfo<>(footprints);
        map.put("list", mapArrayList);
        map.put("pages", marketFootprintPageInfo.getPageNum());
        map.put("total", marketFootprintPageInfo.getTotal());

        return map;
    }

    @Override
    public int deleteFootprint(Integer id) {

        int code = marketFootprintMapper.deleteByPrimaryKey(id);

        return code;
    }

    @Override
    public void insertIntoFootprintList(MarketFootprint marketFootprint) {

        marketFootprintMapper.insert(marketFootprint);
    }
}
